﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Xml;

namespace Algorithm
{
    public class DatabaseVersion
    {


        /// <summary>
        /// Get the Application Server address
        /// </summary>
        /// <param name="configFile">Configuration file</param>
        /// <param name="xpath">Path to node</param>
        /// <returns>Application Server</returns>
        public string GetApplicationServer(string configFile,
                                           string xpath)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(configFile);

            XmlNode root = doc.DocumentElement;
            XmlNode node = root.SelectSingleNode(xpath);

            return node.Attributes["address"].Value;
        }

        public string PacomIsVersion(string sqlServer, string databaseName)
        {
            string version = "0.0.0.0";
            string dbNameString = "master";
            string serverNameString = "localhost";

            string currentConnection = string.Format("Persist Security Info=False;Integrated Security=true;Initial Catalog = {0}; server = {1};", dbNameString, serverNameString);

            using (SqlConnection cn = new SqlConnection(currentConnection))
            {
                string sql = string.Format("SELECT physical_name AS current_file_location FROM sys.master_files where name = '{0}'", dbNameString);//"SELECT TOP (1) CompatibleVersion FROM dbo.ApplicationVersion";
                SqlCommand cmd = new SqlCommand(sql, cn);

                cn.Open();
                version = (string)cmd.ExecuteScalar();
            }
            return version;
        }
    }
}
